/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.plugins.stripes.hyperstripes.core.tags;

import com.farent.solutions.plugins.stripes.hyperstripes.core.ajax.AjaxResponseInsert;
import com.farent.solutions.plugins.stripes.hyperstripes.core.Attribute;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentProcessor;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentTagHandler;
import com.farent.solutions.plugins.stripes.hyperstripes.core.FormChildComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.FormMode;
import com.farent.solutions.plugins.stripes.hyperstripes.core.TagHandler;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.mvel2.templates.CompiledTemplate;
import org.mvel2.templates.TemplateCompiler;
import org.mvel2.templates.TemplateRuntime;

/**
 *
 * @author frenio
 */
@TagHandler("form")
public class FormTagHandler implements ComponentTagHandler {

    @Override
    public String createContent(ComponentModel model, Object bindings ) {
        return processContent(model, bindings);
    }

    public FormMode parseFormMode(List<Attribute> attributes, Object bindings) {
        Attribute attribute = null;
        for (int i = 0; i < attributes.size(); i++) {
            Attribute _attr = attributes.get(i);
            if ( _attr.getName().equalsIgnoreCase("mode") ) {
                attribute = _attr;
                break;
            }
        }
        if( attribute == null )
            return FormMode.EDIT;
        String attributeVal = attribute.getValue();
        if( attributeVal.contains("@{"))
        {
            CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(attributeVal);
            attributeVal = (String) TemplateRuntime.execute(compiledTemplate, bindings);
        }
        return FormMode.parse(attributeVal);
    }

    @Override
    public String createScripts(ComponentModel model) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public AjaxResponseInsert createAjaxInsert(ComponentModel model, Object bindings) {
        AjaxResponseInsert responseInsert = new AjaxResponseInsert();
        String output =  processContent(model, bindings);
        responseInsert.setElementValue(output);
        return responseInsert;
    }

    public String processContent(ComponentModel model, Object bindings) {
        FormComponentModel formModel = (FormComponentModel) model;

        FormMode mode = parseFormMode( formModel.getAttributes(), bindings );
        formModel.setMode(mode);
        List<ComponentModel> children = formModel.getChildren();
        CompiledTemplate compiled3 = null;
        StringBuilder prettyBuilder = new StringBuilder();
        for (int i = 0; i < children.size(); i++) {
            FormChildComponentModel child = (FormChildComponentModel) children.get(i);
            child.setMode(mode);
            String childString = ComponentProcessor.processComponentContent( child, bindings );
            prettyBuilder.append(childString);
        }
        Map variables = new HashMap();
        variables.put("content", prettyBuilder.toString());
        variables.put("model", formModel);
        InputStream template3 = InputTagHandler.class.getResourceAsStream("form.tag");
        compiled3 = TemplateCompiler.compileTemplate(template3);
        return  (String) TemplateRuntime.execute(compiled3, variables);
    }

    @Override
    public void createContent(ComponentModel model, Object bindings, OutputStream outputStream) {
        FormComponentModel formModel = (FormComponentModel) model;

        FormMode mode = parseFormMode( formModel.getAttributes(), bindings );
        formModel.setMode(mode);
        List<ComponentModel> children = formModel.getChildren();
        CompiledTemplate compiled3 = null;
        StringBuilder prettyBuilder = new StringBuilder();
        for (int i = 0; i < children.size(); i++) {
            FormChildComponentModel child = (FormChildComponentModel) children.get(i);
            child.setMode(mode);
            String childString = ComponentProcessor.processComponentContent( child, bindings );
            prettyBuilder.append(childString);
        }
        Map variables = new HashMap();
        variables.put("content", prettyBuilder.toString());
        variables.put("model", formModel);
        InputStream template3 = InputTagHandler.class.getResourceAsStream("form.tag");
        compiled3 = TemplateCompiler.compileTemplate(template3);
        TemplateRuntime.execute(compiled3, variables, outputStream);
    }
}
